package org.jeecg.modules.satff.service.impl;

import org.jeecg.modules.satff.entity.AttendanceGroup;
import org.jeecg.modules.satff.entity.AttendanceClass;
import org.jeecg.modules.satff.entity.AttendanceWorkday;
import org.jeecg.modules.satff.mapper.AttendanceClassMapper;
import org.jeecg.modules.satff.mapper.AttendanceWorkdayMapper;
import org.jeecg.modules.satff.mapper.AttendanceGroupMapper;
import org.jeecg.modules.satff.service.IAttendanceGroupService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 考勤组
 * @Author: nbacheng
 * @Date:   2024-12-14
 * @Version: V1.0
 */
@Service
public class AttendanceGroupServiceImpl extends ServiceImpl<AttendanceGroupMapper, AttendanceGroup> implements IAttendanceGroupService {

	@Autowired
	private AttendanceGroupMapper attendanceGroupMapper;
	@Autowired
	private AttendanceClassMapper attendanceClassMapper;
	@Autowired
	private AttendanceWorkdayMapper attendanceWorkdayMapper;
	
	@Override
	@Transactional
	public void saveMain(AttendanceGroup attendanceGroup, List<AttendanceClass> attendanceClassList,List<AttendanceWorkday> attendanceWorkdayList) {
		attendanceGroupMapper.insert(attendanceGroup);
		if(attendanceClassList!=null && attendanceClassList.size()>0) {
			for(AttendanceClass entity:attendanceClassList) {
				//外键设置
				entity.setAttendanceGroupId(attendanceGroup.getId());
				attendanceClassMapper.insert(entity);
			}
		}
		if(attendanceWorkdayList!=null && attendanceWorkdayList.size()>0) {
			for(AttendanceWorkday entity:attendanceWorkdayList) {
				//外键设置
				entity.setAttendanceGroupId(attendanceGroup.getId());
				attendanceWorkdayMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AttendanceGroup attendanceGroup,List<AttendanceClass> attendanceClassList,List<AttendanceWorkday> attendanceWorkdayList) {
		attendanceGroupMapper.updateById(attendanceGroup);
		
		//1.先删除子表数据
		attendanceClassMapper.deleteByMainId(attendanceGroup.getId());
		attendanceWorkdayMapper.deleteByMainId(attendanceGroup.getId());
		
		//2.子表数据重新插入
		if(attendanceClassList!=null && attendanceClassList.size()>0) {
			for(AttendanceClass entity:attendanceClassList) {
				//外键设置
				entity.setAttendanceGroupId(attendanceGroup.getId());
				attendanceClassMapper.insert(entity);
			}
		}
		if(attendanceWorkdayList!=null && attendanceWorkdayList.size()>0) {
			for(AttendanceWorkday entity:attendanceWorkdayList) {
				//外键设置
				entity.setAttendanceGroupId(attendanceGroup.getId());
				attendanceWorkdayMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		attendanceClassMapper.deleteByMainId(id);
		attendanceWorkdayMapper.deleteByMainId(id);
		attendanceGroupMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			attendanceClassMapper.deleteByMainId(id.toString());
			attendanceWorkdayMapper.deleteByMainId(id.toString());
			attendanceGroupMapper.deleteById(id);
		}
	}
	
}
